home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / basic.arc / A4.TXT < prev    next >
Encoding:
Text File  |  1986-04-26  |  11.4 KB  |  251 lines

  1. oing on to lesson four.
  2.  
  3.     If you are using the BASIC Prof,        |The PC-Prof.~
  4. let me know who you are!  Send your name        |P.O. Box 26~
  5. and address to:                        |Salina, Kansas~
  6.                             |67402-0026~
  7.  
  8.     If you like the Prof, include a contribution ($30 - $50 suggested) to
  9. help support development of additional volumes.  Please copy and share the
  10. Prof. with other IBM P.C. users.
  11. -----
  12. -----
  13. REM Statement
  14.  
  15.         All right!  You have successfully completed three lessons in BASIC.  By
  16. now you should be fairly confident with the language.
  17.  
  18.         The |REM~ statement allows you to insert explanatory |rem~arks into the
  19. program.  These remarks are |ignored~ by the computer when it executes the
  20. program, but provide the programmer with information about the program.  They
  21. appear only when you LIST the program.  The REM statement can begin with either
  22. the letters |REM~ or an apostrophe (|'~) followed by the remark itself.
  23.  
  24.         Here is an example of REM statements in a program.
  25.  
  26.                 10 DATA 45,24,97,65,47,15,80,38,77,999
  27.                 20 READ X:IF X=999 THEN 70      |'If end of data goto line 60.~
  28.                 30 IF X>=50 THEN LET G=G+1      |'Count numbers G.T.E. to 50.~
  29.                 40 IF X<50 THEN LET L=L+1       |'Count number L.T. 50.~
  30.                 50 GOTO 20                      |'Get the next number.~
  31.                 60 PRINT "Of the numbers,";L;"were less than 50 and";
  32.                 70 PRINT G;"were greater than or equal to 50."
  33.  
  34.         Try typing in and |RUN~ning this program.
  35. -----
  36. STOP and END Statements
  37.  
  38.         There are four ways to stop the execution of a program in BASIC.
  39.  
  40.         1.  The program automatically stops when it runs out of lines to
  41.             execute.
  42.         2.  |STOP~ Statement.  When the computer comes to a STOP statement, it
  43.             prints |Break in N~ (where |N~ is the |line number~ of the STOP
  44.             statement) and stops.
  45.         3.  |END~ Statement.  This is similar to the STOP statement except that
  46.             the computer does not print the Break message.
  47.         4.  Pressing the |Ctrl~ key and the |Break~ key (Scroll lock) together
  48.             stops the program during execution and prints a |Break in N~
  49.             message.  If you want to continue with the program, simply type
  50.             |CONT~ (short for |cont~inue) and press the return key.  If you haven't
  51.             edited the program, the computer will continue with the program
  52.             right where it was interrupted (this works if you used a STOP
  53.             statement as well, but not if you used an END statement).
  54. -----
  55. STOP and END Statements (continued)
  56.  
  57.         Try out these statements and commands by typing in and |RUN~ning the
  58. following program.
  59.  
  60.                 |10 PRINT "Type CONT and press return."~
  61.                 |20 STOP~
  62.                 |30 PRINT "GOOD!  You continued the program."~
  63.                 |40 PRINT "Use the Ctrl and Break keys any time now!":GOTO 40~
  64. -----
  65. EDIT Command
  66.  
  67.         When you make (or discover) a mistake in a program line, you can
  68. correct it by simply typing the line over.  However, it is much faster to use
  69. the BASIC Program Editor.  To do this, simply type |EDIT N~ (where |N~ is the line
  70. number to edit) and then move to the mistake using the cursor control keys, and
  71. correct it by typing over, inserting or deleting.
  72.  
  73.         If you want to completely erase that line of the program, you can do so
  74. by typing the line number itself and pressing return.  If there are several
  75. lines you want to delete, type |DELETE N1-N2~ (where |N1~ is the first line and |N2~
  76. is the last line to delete) and press return.  This will delete all lines
  77. between and including |N1~ and |N2~.
  78. -----
  79. AUTO Command
  80.  
  81.         When you are first writing a program, you spend a good deal of time
  82. just typing in the line numbers.  The |AUTO~ command will take care of that for
  83. you.  Type the word AUTO and the computer will put the line numbers in
  84. automatically.
  85.  
  86.         Here is the correct form, or syntax, for the AUTO command.
  87.  
  88.                                 AUTO [|B~[,|I~]]
  89.  
  90.         |B~ is the line number at which the AUTO command will begin.  If you do
  91. not give a value for |B~ the computer will |default~ to (automatically start
  92. at) line number 10.
  93.  
  94.         |I~ is the increment value.  It is the value that will be added to each
  95. line number to get the next line number.  If you do not enter a number for |I~,
  96. it will default to 10.
  97. -----
  98. AUTO Command (continued)
  99.  
  100.         If you want to start at some line number other than 10, say 200, type
  101. AUTO 200 and your line numbers will begin at 200 and increase in increments of
  102. 10.  If you want to start at 100 and increase by 5's then type AUTO 100,5.
  103. When you finish typing in your program, shut off the AUTO command by pressing
  104. the |Ctrl~ key and the |Break~ key together.
  105.  
  106.         Try typing in the following program using the AUTO command.
  107.  
  108.                 |100 INPUT "What is the first number";A~
  109.                 |105 INPUT "What is the second number";B~
  110.                 |110 PRINT A;"+";"B";"=";A+B~
  111.                 |115 PRINT A;"-";"B";"=";A-B~
  112.                 |120 PRINT A;"*";"B";"=";A*B~
  113. -----
  114. TRON and TROFF Commands
  115.  
  116.         If a program doesn't work properly when you try running it, it is said
  117. to have a |bug~ in it.  Much of a programmer's time is spent |de-bugging~ his
  118. or her programs.  A feature that sometimes makes this easier is the |TRON~
  119. command.  TRON is short for program |TR~acer |ON~.  When you type TRON and then RUN
  120. your program, the computer will print each line number, in brackets (|[]~) as
  121. it is executed.  Other program output will also be printed, but only the
  122. executed line numbers will be printed in brackets.
  123.  
  124.         For this program:       |10 DATA 17,23,99~
  125.                                 |20 READ NUM~
  126.                                 |30 IF NUM<>99 THEN PRINT NUM:GOTO 20~
  127.                                 |40 PRINT "The END."~
  128.         The output using TRON
  129.         would look like this:   |[10][20][30] 17~
  130.                                 |[20][30] 23~
  131.                                 |[20][30][40]The END.~
  132.  
  133.         When you have found your bug and want to run your program without the
  134. line numbers being displayed, type |TROFF~ which is short for program |TR~acer |OFF~.
  135. This returns the computer to normal program execution.
  136. -----
  137. TRON and TROFF Commands (continued)
  138.  
  139.         The following program is supposed to count and print the values in a
  140. DATA statement, but for some reason it gets stuck on the first value and goes
  141. crazy.  By using the TRON and TROFF commands, try and find the bug in this
  142. program.
  143.  
  144.                 |10 DATA 34,67,12,9,65,38,84,999~
  145.                 |20 READ NUM:IF NUM=999 THEN GOTO 60~
  146.                 |30 COUNT=COUNT+1~
  147.                 |40 PRINT "Value number";COUNT;"is";NUM~
  148.                 |50 GOTO 30~
  149.                 |60 PRINT "There are";COUNT;"numbers in the data statement."~
  150.  
  151.         First, type in the program and try to |RUN~ it.  After a few seconds
  152. press the |Ctrl~ and |Break~ keys to stop the program.  Then type |TRON~ to turn the
  153. tracer on.  Again |RUN~ the program and when you have identified the problem,
  154. stop the program and fix the bug.  Now turn the tracer off by typing |TRON~ and
  155. |RUN~ the program a third time.
  156. -----
  157. TRON and TROFF Commands (continued)
  158.  
  159.         Here is the same program from the last page.  Hopefully you found the
  160. bug in line |50~.
  161.  
  162.                 10 DATA 34,67,12,9,65,38,84,999
  163.                 20 READ NUM:IF NUM=999 THEN GOTO 60
  164.                 30 COUNT=COUNT+1
  165.                 40 PRINT "Value number";COUNT;"is";NUM
  166.                 |50 GOTO 30~
  167.                 60 PRINT "There are";COUNT;"numbers in the data statement."
  168.  
  169.         The line should have read |50 GOTO 20~.  As it was, the program never
  170. read a new value for the variable |NUM~, so it never got to |999~ to signal the
  171. |end of DATA~.
  172.  
  173.         If you found the bug, congratulations!  If not make sure you understand
  174. how the TRON and TROFF commands work and better luck next time.
  175. -----
  176. RND Function
  177.  
  178.         The |RND~ function returns a random number between 0 and 1.  If you
  179. want a random number between 0 and 1000, then just multiply by 1000.
  180.  
  181.                 example- LET |X=RND~       [this returns |X~ such that |0~ < |X~ <| 1~]
  182.                          LET |Y=RND*100~   [this returns |Y~ such that |0 ~< |Y~ <| 100~]
  183.  
  184.         Here is a sample program that lets you input an |upper bound~ number,
  185. and the computer prints a list of random numbers between 0 and your upper
  186. bound.  You will have to use the |Ctrl~ and |Break~ keys to stop this program.
  187.  
  188.                 |10 INPUT "Enter a number to be the upper bound.",MAX~
  189.                 |20 PRINT RND*MAX~
  190.                 |30 GOTO 20~
  191. -----
  192. INT Function
  193.  
  194.         The |INT~ function returns the largest integer less than or equal to
  195. the number in parentheses.
  196.  
  197.                 example- |LET A=INT(9.2)~    [in this statement, |A~ will equal  |9~]
  198.                          |LET K=INT(-7.6)~   [in this statement, |K~ will equal |-8~]
  199.  
  200.         Here is a program that uses the RND and INT functions to quiz you on
  201. the INT function.  Once again, press the |Ctrl~ and |Break~ keys when you have
  202. had enough.
  203.  
  204.         |10 NUMBER=RND*200-100~ 'pick a random number between -100 and 100
  205.         |20 PRINT "What is the INT of";NUMBER;:INPUT ANSWER~
  206.         |30 IF ANSWER<>INT(NUMBER) THEN PRINT "The answer is";INT(NUMBER)~
  207.         |40 GOTO 10~
  208. -----
  209. ABS Function
  210.  
  211.         The |ABS~ function returns the |absolute value~ of an expression.  The
  212. absolute value is just the positive value of a number.
  213.  
  214.                 example- |LET X=ABS(-5)~    [in this statement, |X~ will equal  |5~]
  215.                          |LET C=ABS(12*4)~  [in this statement, |C~ will equal |48~]
  216.  
  217.         Here is a program that uses the RND, INT and ABS functions to quiz you
  218. on the ABS function.  Once again, press the |Ctrl~ and |Break~ keys when you
  219. have had enough.
  220.  
  221.         |10 NUMBER=INT(RND*200-100)~ 'pick integer between -100 and 100
  222.         |20 PRINT "What is the ABS of";NUMBER;:INPUT ANSWER~
  223.         |30 IF ANSWER<>ABS(NUMBER) THEN PRINT "The answer is";ABS(NUMBER)~
  224.         |40 GOTO 10~
  225. -----
  226. ABS and INT functions (continued)
  227.  
  228.         Here is another short program that you can try out.  It uses the ABS
  229. and INT functions to find the |G~reatest |C~ommon |D~enominator of two numbers.
  230.  
  231.                 |10 INPUT "Enter the first number.",NUM1~
  232.                 |20 INPUT "Enter the second number.",NUM2~
  233.                 |30 NUM1=ABS(NUM1):NUM2=ABS(NUM2)~
  234.                 |40 LET R=NUM1-NUM2*INT(NUM1/NUM2)~
  235.                 |50 IF R=0 THEN GOTO 70~
  236.                 |60 NUM1=NUM2:NUM2=R:GOTO 40~
  237.                 |70 PRINT "The greatest common denominator is";NUM2~
  238. -----
  239. End of Lesson Four
  240.  
  241.         Congratulations on completing the |Beginning BASIC~ Course.  Now to bring
  242. together much of what you've learned, try writing the following programs.
  243.  
  244.         1. A program that will find the average and the sum of a series of
  245.            numbers in a DATA statement.
  246.         2. A number guessing game program.  (|Hint~: Have the computer pick a
  247.            number between 1 and 1000 and then INPUT guesses as to what the
  248.            number is.)
  249.  
  250. If you have trouble, type  |LOAD"AVERAGE"~ for the average program,
  251.